package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.SkuAttrValueService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/admin/product")
@RestController
public class SkuController {

    @Autowired
    SkuInfoService skuInfoService;

    /**
     * 获取sku分页列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuPage(@PathVariable("page") Long pageNum,
                             @PathVariable("limit") long pageSize){

        Page<SkuInfo> skuInfoPage = new Page<>(pageNum, pageSize);
        IPage<SkuInfo> page = skuInfoService.page(skuInfoPage);
        return Result.ok(page);
    }

    /**
     * 添加sku
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){

        skuInfoService.saveSkuInfo(skuInfo);

        return Result.ok();
    }

    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        skuInfoService.upOrDown(skuId,1);
        return Result.ok();
    }

    /**
     * 商品下架
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        skuInfoService.upOrDown(skuId,0);
        return Result.ok();
    }
}
